package controller;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ViewResolver;

import pojo.Book;

@SessionAttributes(value = { "bk" })
@Controller
@RequestMapping(value = "customers")
public class CustomerController {

	@ModelAttribute
	public void addAge(Model model) {
		System.out.println("add age.....");
		model.addAttribute("age", 100);
	}

	@ModelAttribute("author")
	public String addAuthor() {
		//
		return "Tom Li";
	}

	@GetMapping("work1")
	public String work1(Model model) {

		model.addAttribute("cname", "johnyu");
		return "suc";
	}

	@GetMapping("work2")
	public String work2(@ModelAttribute(value = "bk") Book book, SessionStatus status) {
		book.setId(123);
		status.setComplete();
		return "suc";
	}

	@GetMapping("work3")
	public String work3(Book book, Model model, HttpSession sen) {
		sen.getServletContext().setAttribute("global", "God");
		model.addAttribute("bk", book);
		return "forward:work4";
	}

	@GetMapping("work4")
	public String work4() {
		System.out.println("work4......");
		return "suc";
	}

	@GetMapping("work5")
	public void work5(OutputStream out, HttpSession sen, HttpServletResponse resp) throws Exception {
		resp.addHeader("Content-Type", "image/png");
		String path = sen.getServletContext().getRealPath("/upload/sprite1.png");
		InputStream in = new FileInputStream(path);
		byte[] buf = new byte[1024];
		int len = 0;
		while ((len = in.read(buf)) != -1) {
			out.write(buf, 0, len);
		}
		in.close();
		out.close();
	}

	@GetMapping("work6")
	public void work6(OutputStream out, HttpSession sen, HttpServletResponse resp) throws Exception {
		resp.addHeader("Content-Disposition", "attchment;filename=aa.png");
		String path = sen.getServletContext().getRealPath("/upload/sprite1.png");
		InputStream in = new FileInputStream(path);
		byte[] buf = new byte[1024];
		int len = 0;
		while ((len = in.read(buf)) != -1) {
			out.write(buf, 0, len);
		}
		in.close();
		out.close();
	}

	@RequestMapping("work7")
	public String work7(List<MultipartFile> heads, String uname) {
		for (MultipartFile file : heads) {
			System.out.println(file.getOriginalFilename());
			System.out.println(file.getName());
			try {
				System.out.println(file.getBytes().length);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		// System.out.println(file.getInputStream());
		return uname;
	}

}



